package com.colin.library.help;


import android.support.annotation.NonNull;


/**
 * Created by Colin on 2016/12/29.
 */
public class NumberHelp {

    private static volatile NumberHelp singleton;

    private NumberHelp() {
    }

    public static NumberHelp getInstance() {
        if (singleton == null) {
            synchronized (NumberHelp.class) {
                if (singleton == null) {
                    singleton = new NumberHelp();
                }
            }
        }
        return singleton;
    }

    /**
     * 默认返回 整形值
     *
     * @param value
     * @return
     */
    public Object getIntValue(@NonNull Object value) {
        return getValue(value, 0);
    }

    public Object getDoubleValue(@NonNull Object value) {
        return getValue(value, 0D);
    }

    public Object getFloatValue(@NonNull Object value) {
        return getValue(value, 0F);
    }

    public Object getLongValue(@NonNull Object value) {
        return getValue(value, 0L);
    }

    /**
     * @param value        输入值
     * @param defaultValue 确定返回值类型
     * @return
     */
    public Object getValue(@NonNull Object value, @NonNull Object defaultValue) {
        if (!RegulatorHelp.getInstance().isNumber(String.valueOf(value))) {
            return defaultValue;
        }
        if (defaultValue instanceof Integer && RegulatorHelp.getInstance().isInteger(String.valueOf(value))) {
            return Integer.valueOf(String.valueOf(value));
        } else if (defaultValue instanceof Float) {
            return Float.valueOf(String.valueOf(value));
        } else if (defaultValue instanceof Double) {
            return Double.valueOf(String.valueOf(value));
        } else if (defaultValue instanceof Long && RegulatorHelp.getInstance().isInteger(String.valueOf(value))) {
            return Long.valueOf(String.valueOf(value));
        } else {
            return defaultValue;
        }
    }
}
